using System;
using System.Globalization;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenWaves.Web;

namespace OpenWaves.Pagination.Controls
{
    public class PageButton : Control, INamingContainer
    {
        public new PageViewModel Page { get; set; }

        public string Text { get; set; }
        public string CssClass { get; set; }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            if (this.Visible && this.CurrentTemplate != null)
            {
                var pagination = this.GetAncestors().OfType<Pager>().First();

                switch(this.CurrentTemplate)
                {
                    case Template.Available:
                        pagination.PageButtonTemplate.InstantiateIn(this);
                        break;
                    case Template.Unavailable:
                        pagination.UnavailablePageButtonTemplate.InstantiateIn(this);
                        break;
                    case Template.Selected:
                        pagination.SelectedPageButtonTemplate.InstantiateIn(this);
                        break;
                    default:
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, 
                            "Unexpected value of CurrentTemplate: {0}", this.CurrentTemplate));
                }
            }
        }

        private Template? CurrentTemplate
        {
            get { return (Template?) this.ViewState["Template"]; }
            set { this.ViewState["Template"] = value; }
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            this.Controls.Clear();

            var pagination = this.GetAncestors().OfType<Pager>().First();

            this.Visible = true;

            if (this.Page == null)
            {
                if (pagination.UnavailablePageButtonTemplate == null)
                {
                    this.Visible = false;
                }
                else
                {
                    this.CurrentTemplate = Template.Unavailable;
                    pagination.UnavailablePageButtonTemplate.InstantiateIn(this);
                    this.DataBindChildren();
                }
            }
            else if (this.Page.IsCurrent)
            {
                var template = pagination.SelectedPageButtonTemplate ?? pagination.PageButtonTemplate;
                if (template != null)
                {
                    this.CurrentTemplate = Template.Selected;
                    template.InstantiateIn(this);
                    this.DataBindChildren();
                }
            }
            else
            {
                if (pagination.PageButtonTemplate != null)
                {
                    this.CurrentTemplate = Template.Available;
                    pagination.PageButtonTemplate.InstantiateIn(this);
                    this.DataBindChildren();
                }
            }

            this.ChildControlsCreated = true;
        }

        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            var commandEventArgs = args as CommandEventArgs;
            if (commandEventArgs != null)
                this.RaiseBubbleEvent(this, new PaginationCommandEventArgs(this, source, commandEventArgs));

            return base.OnBubbleEvent(source, args);
        }

        private enum Template
        {
            Available,
            Unavailable,
            Selected
        }
    }
}